/**
 * 展开下级所有节点
 * @param node
 */
function expandChildNodes(node) {
  if (Array.isArray(node.childNodes)) {
    let len = node.childNodes.length
    for (let i = 0; i < len; i++) {
      let child = node.childNodes[i]
      child.expanded = true
      expandChildNodes(child)
    }
  }
}
/**
 * 在二级节点下加入空的子节点，否则二级节点无法显示打开图标
 * @param taskList
 * @returns {Array}
 */
function appendChildren(taskList) {
  taskList.map(item => {
    item.children.map(item => {
      item.children = [{label: ''}]
    })
  })
  return taskList
}

/**
 * 获取点击节点下所有层级的工单ID
 * @param data 当前节点树数组
 * @param workorderIds 容器空数组
 * @returns {*} 当前节点下所有工单id
 */
function getWorkOrderIds(data, workorderIds) {
  for (let i = 0; i < data.length; i++) {
    let item = data[i]
    if (item.workorders) {
      item.workorders.map(workorder => {
        workorderIds.push(workorder.id)
      })
    }
    if (item.children) {
      getWorkOrderIds(item.children, workorderIds)
    }
  }
  return workorderIds
}

const treeUtil = {expandChildNodes, appendChildren, getWorkOrderIds}

export default treeUtil
